<template>
  <el-drawer
    :visible.sync="drawer"
    :direction="direction"
    size="40%"
    :close-on-click-modal="false"
    :wrapper-closable="false"
    :before-close="handleClose">
    <div class="content">
      <div class="title-head">
        编辑
        <div class="but">
          <el-button @click="handleClose" size="mini">取 消</el-button>
          <el-button type="primary" @click="save" size="mini">保存</el-button>
        </div>
      </div>
      <div class="form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="125px" class="demo-ruleForm">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="系统项目名称" prop="cProNme">
                <el-input v-model="ruleForm.cProNme" placeholder="请输入系统项目名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目状态" prop="cProState">
                <el-select v-model="ruleForm.cProState" placeholder="请选择项目状态" @change="rulesState">
                  <el-option v-for="item in cProStateOption" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建人" prop="cCrePer">
                <el-select disabled v-model="ruleForm.cCrePer" placeholder="未设置" style="width: 100%">
                  <el-option v-for="item in cProHeadOption" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目负责人" prop="cProHead">
                <el-select v-model="ruleForm.cProHead" placeholder="未设置" style="width: 100%">
                  <el-option v-for="item in cProHeadOption" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="使用角色" prop="cUseRoles">
                <el-input v-model="ruleForm.cUseRoles" placeholder="请输入使用角色"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="业务负责人" prop="cBusHead">
                <el-select v-model="ruleForm.cBusHead" placeholder="请选择业务负责人" style="width: 100%">
                  <el-option v-for="item in cProHeadOption" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="技术负责人" prop="cTechHead">
                <el-select v-model="ruleForm.cTechHead" placeholder="请选择技术负责人" style="width: 100%">
                  <el-option v-for="item in cProHeadOption" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="测试负责人" prop="cTestHead">
                <el-select v-model="ruleForm.cTestHead" placeholder="请选择测试负责人" style="width: 100%">
                  <el-option v-for="item in cProHeadOption" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="运维负责人" prop="cOmHead">
                <el-select v-model="ruleForm.cOmHead" placeholder="请选择运维负责人" style="width: 100%">
                  <el-option v-for="item in cProHeadOption" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应用访问地址" prop="cProAddr">
                <el-input v-model="ruleForm.cProAddr" placeholder="请输入应用访问地址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应用开发环境" prop="cDevAddr">
                <el-input v-model="ruleForm.cDevAddr" placeholder="请输入应用开发环境"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应用测试环境字段" prop="cTestAddr">
                <el-input v-model="ruleForm.cTestAddr" placeholder="请输入应用测试环境字段"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参与者">
                <el-cascader
                  style="width: 100%"
                  @change="handleParticipant"
                  :show-all-levels="false"
                  v-model="participantList"
                  :options="allPersonnel"
                  :props="{multiple: true,label: 'postName', children: 'users', value: 'postId'}"
                  placeholder="未设置"
                  clearable></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建时间" prop="tCreTm">
                <el-date-picker
                  disabled
                  style="width: 100%"
                  v-model="ruleForm.tCreTm"
                  type="datetime"
                  placeholder="请选择创建时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计划开始时间" prop="tCreTm">
                <el-date-picker
                  style="width: 100%"
                  v-model="ruleForm.tStaTm"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  type="datetime"
                  placeholder="请选择创建时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计划完成时间" prop="tCreTm">
                <el-date-picker
                  style="width: 100%"
                  v-model="ruleForm.tEndTm"
                  type="datetime"
                  placeholder="请选择创建时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应用详情" prop="cUseRoles">
                <el-upload
                  class="upload-demo"
                  :before-upload="handelUpLoad"
                  action="#"
                  list-type="picture-card"
                  :http-request="requestUpload"
                  multiple>
                  <i class="el-icon-upload2 icon"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import {mixin} from "@/views/projectManage/mixin";
import {emitList} from "@/api/projectManage/project";
import {upload} from "@/api/projectManage/workItem";

export default {
  name: "upDateForm",
  mixins: [mixin],
  data() {
    return {
      drawer: false,
      direction: 'rtl',
      disabled: false,
      ruleForm: {},
      rules: {
        cProNme: [
          { required: true, message: '请输入系统项目名称', trigger: 'blur' },
        ],
        cProHead: [
          { required: true, message: '请选择项目状态', trigger: 'change' },
        ],
      },
      participantList: [],
      upLoadImg: null
    }
  },
  methods: {
    // 打开弹窗
    open({row}) {
      console.log(row)
      this.getTree()
      this.getMember()
      this.drawer = true
      const obj = JSON.parse(JSON.stringify(row))
      this.ruleForm = {
        ...obj,
        cCrePer: Number(obj.cCrePer),
        cProHead: Number(obj.cProHead),
        cTechHead: Number(obj.cTechHead),
        cBusHead: Number(obj.cBusHead),
        cTestHead: Number(obj.cTestHead),
        cOmHead: Number(obj.cOmHead),
      }
    },
    // 校验项目状态
    rulesState() {
      // 当项目状态修改为已上线时
      if (this.ruleForm.cProState === '5') {
        // 需校验以下四个字段都已填写
        if (!this.ruleForm.cProAddr || !this.ruleForm.cDevAddr ||
          !this.ruleForm.cTestAddr || !this.upLoadImg) {
          this.$notify({
            title: '警告',
            message: '当项目状态修改为已上线时，(应用访问地址、应用开发环境、应用测试环境字段，应用详情)不可为空！',
            type: 'warning',
            duration: 8000
          })
          this.ruleForm.cProState = ''
        }
      }
    },
    // 关闭
    handleClose() {
      this.drawer = false
      this.$refs['ruleForm'].resetFields();
      this.ruleForm = {}
    },
    // 文件上传前
    handelUpLoad(file) {
      console.log(file)
    },
    // 自定义文件上传地址
    requestUpload(params) {
      console.log(params)
      const data = new FormData()//自定义 参数类型必须为 FormData格式 此为强制要求
      data.append("file", params.file)
      data.append("cPmNo", this.projectInfo.cProNo)
      this.upLoadImg = params.file
      upload(data).then(res => {
        this.$notify({title: '成功', message: '上传成功！', type: 'success',});
      })
    },
    // 保存
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          emitList(this.ruleForm).then(res => {
            this.$notify({title: '成功', message: '编辑成功！', type: 'success',})
            this.handleClose()
            this.$emit('upDate')
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 选择参与者
    handleParticipant(val) {
      const list = []
      val.forEach(i => list.push(i[1]))
      this.ruleForm.cPrdPer = list.join(',')
      this.ruleForm.cTxt = JSON.stringify(val)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
.content {
  padding: 20px 30px;
  .form-box {
    padding: 20px 0;
  }
}
.upload-demo {
  .icon {
    margin-top: 30%;
    font-size: 22px;
    color: #979797;
  }
  .text {
    line-height: 15px;
    font-size: 12px;
    font-weight: normal;
    color: #979797;
  }
}
::v-deep .el-upload-dragger {
  width: 109px;
  height: 109px;
  background: #F8F9FA;
}

</style>
